package top.chenbn.aclservice.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.chenbn.aclservice.entity.Permission;
import top.chenbn.aclservice.service.PermissionService;
import top.chenbn.guli.common.util.Result;

import java.util.List;

/**
 * 权限 菜单管理
 *
 * @author testjava
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/admin/acl/permission")
// @CrossOrigin
// @Api(value = "PermissionController Api", tags = "PermissionController")
public class PermissionController {

  @Autowired private PermissionService permissionService;

  // 获取全部菜单
  @ApiOperation(value = "查询所有菜单")
  @GetMapping
  public Result indexAllPermission() {
    List<Permission> list = permissionService.queryAllMenuGuli();
    return Result.ok().data("children", list);
  }

  @ApiOperation(value = "递归删除菜单")
  @DeleteMapping("remove/{id}")
  public Result remove(@PathVariable String id) {
    permissionService.removeChildByIdGuli(id);
    return Result.ok();
  }

  @ApiOperation(value = "给角色分配权限")
  @PostMapping("/doAssign")
  public Result doAssign(String roleId, String[] permissionId) {
    permissionService.saveRolePermissionRealtionShipGuli(roleId, permissionId);
    return Result.ok();
  }

  @ApiOperation(value = "根据角色获取菜单")
  @GetMapping("toAssign/{roleId}")
  public Result toAssign(@PathVariable String roleId) {
    List<Permission> list = permissionService.selectAllMenu(roleId);
    return Result.ok().data("children", list);
  }

  @ApiOperation(value = "新增菜单")
  @PostMapping("save")
  public Result save(@RequestBody Permission permission) {
    permissionService.save(permission);
    return Result.ok();
  }

  @ApiOperation(value = "修改菜单")
  @PutMapping("update")
  public Result updateById(@RequestBody Permission permission) {
    permissionService.updateById(permission);
    return Result.ok();
  }
}
